﻿using System;
using System.Linq;
using KharaPos.Web.Models.Shared;

namespace KharaPos.Web.Entities
{
    public partial class BusinessUnit
    {
        public void SelectSeasonalProductsForBusinessUnit(DateTime seasonStart, DateTime seasonEnd)
        {
            //get the total sales for the season
            var totalSales = (from sale in Sales
                             where sale.DateOfSale > seasonStart
                                   && sale.DateOfSale < seasonEnd
                             select sale.LineItems.Sum(line => line.Cost)).Sum(total=>total);
            //get the manufacturers for the business unit
            var manufacturers =
                Catalogs.Select(c => c.Product.ManuFacturer).Distinct(new Equality<ManuFacturer>(i => i.Id));
            //Group the sales by manufacturer
            var salesByManufacturer = 
                (from sale in Sales
                 where sale.DateOfSale > seasonStart
                 && sale.DateOfSale < seasonEnd
                 from lineitem in sale.LineItems
                 join manuFacturer in manufacturers on lineitem.Product.ManufacturerId equals manuFacturer.Id
                 select new
                 {
                     Manfacturer = manuFacturer,
                     Amount = lineitem.Cost
                 }).GroupBy(i => i.Manfacturer);
            foreach (var group in salesByManufacturer)
            {
                var manufacturer = group.Key;
                var pct = group.Sum(t => t.Amount)/totalSales;
                SelectCatalogItemsBasedOnPercentage(manufacturer, pct);
            }
        }

        private void SelectCatalogItemsBasedOnPercentage(ManuFacturer manufacturer, decimal pct)
        {
            ///Rest of logic here.
        }
    }
}